package com.alibaba.citrus.cr.mn.order.center.facade.adapter.impl;

import com.alibaba.citrus.cr.mn.order.center.facade.adapter.SoldToPartyAbilityAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.request.SoldToPartyByAccurateQueryRequest;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.SoldToPartyByAccurateQueryDTO;
import com.alibaba.cloud.retail.customer.center.ability.SoldToPartyAbilityService;
import com.alibaba.cloud.retail.customer.center.req.SoldToPartyAccurateQueryReq;
import com.alibaba.cloud.retail.customer.center.resp.SoldToPartyInternalResp;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lydass.bizphin.maca.model.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName SoldToPartyAbilityAdapterImpl
 * @description: TODO
 * @date 2023/05/30 17:03
 * @version: 1.0
 */

@Service
public class SoldToPartyAbilityAdapterImpl implements SoldToPartyAbilityAdapter {
    private static final Log log = Log.getLogger(SoldToPartyAbilityAdapterImpl.class);

    @DubboReference
    private SoldToPartyAbilityService soldToPartyAbilityService;

    @Override
    public List<SoldToPartyByAccurateQueryDTO> querySoldToPartyByAccurate(List<SoldToPartyByAccurateQueryRequest> request) {

        if (CollectionUtils.isEmpty(request)) {
            return Lists.newArrayList();
        }

        List<SoldToPartyAccurateQueryReq> reqList = request.stream().map(p -> {
            SoldToPartyAccurateQueryReq req = new SoldToPartyAccurateQueryReq();
            req.setBusinessType(p.getBusinessType());
            req.setSoldToPartyCode(p.getSoldToPartyCode());
            req.setSalesChannelCode(p.getSalesChannelCode());
            req.setSalesOrganizationCode(p.getSalesOrganizationCode());
            return req;
        }).collect(Collectors.toList());
        Result<List<SoldToPartyInternalResp>> listResult = soldToPartyAbilityService.querySoldToPartyList(reqList);
        log.info("售达方查询 request={}, result={}", JSONArray.toJSONString(reqList), JSONObject.toJSONString(listResult));
        if (Objects.isNull(listResult) || CollectionUtils.isEmpty(listResult.getResult())) {
            return Lists.newArrayList();
        }
        return listResult.getResult()
                .stream()
//                .filter(resp -> "1".equals(resp.getStatus()))
                .map(p -> {
                    SoldToPartyByAccurateQueryDTO queryDTO = new SoldToPartyByAccurateQueryDTO();
                    BeanUtils.copyProperties(p, queryDTO);
                    return queryDTO;
                }).collect(Collectors.toList());
    }
}
